package com.xuecheng.search.controller;

import com.xuecheng.api.search.EsCourseControllerApi;
import com.xuecheng.framework.domain.course.CoursePub;
import com.xuecheng.framework.domain.course.response.TeachplanMediaPub;
import com.xuecheng.framework.domain.search.CourseSearchParam;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.search.service.EsCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

//搜索课程信息Day11
@RestController
@RequestMapping("/search/course")
public class EsCourseController implements EsCourseControllerApi {
    //搜索课程信息Day11
    @Autowired
    EsCourseService esCourseService;

    //搜索课程信息Day11   CourseSearchParam:查询条件 价格 难度级别等等    QueryResponseResult:常用分页返回类型
    @GetMapping(value = "/list/{page}/{size}")
    public QueryResponseResult<CoursePub> list(@PathVariable("page") int page,@PathVariable("size")  int size, CourseSearchParam courseSearchParam) {
        return esCourseService.list(page,size,courseSearchParam);
    }


    //Day_15_2 根据id查询课程信息
    @GetMapping("/getall/{id}")
    public Map<String, CoursePub> getall(@PathVariable("id") String id) {
        return esCourseService.getall(id);
    }


    //Day_18@4:当前用户拥有se_teach_meizi权限是方可访问此方法
    //*(因为:course服务有问题,我人为加载SE服务中.  Spring Security的注解控制)
    @PreAuthorize("hasAuthority('se_teach_meizi')")

    //Day_15@9:根据课程计划id查询,课程媒资信息
    @GetMapping(value="/getmedia/{teachplanId}")
    public TeachplanMediaPub getmedia(@PathVariable("teachplanId") String teachplanId) {
        //第一件事:将一个id加入数组，传给service方法   -- 提高扩展性
        String[] teachplanIds = new String[]{teachplanId};
        QueryResponseResult<TeachplanMediaPub> queryResponseResult = esCourseService.getmedia(teachplanIds);
        //第二件事:获取返回集合中的数据
        QueryResult<TeachplanMediaPub> queryResult = queryResponseResult.getQueryResult();
        if(queryResult != null){
            List<TeachplanMediaPub> list = queryResult.getList();
            if(list !=null && list.size()>0){
                //看似是返回多条数据,get(0) 拿第一个对象数据,  其实还是第一个对象
                return list.get(0);
            }
        }
        //如果没有查询到,返回空对象
        return new TeachplanMediaPub();
    }


}
